﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.IO;
using System.Threading.Tasks;

namespace KeanuMvc.Reference {

    public class JpegMediaTypeFormatter : MediaTypeFormatter {

        public JpegMediaTypeFormatter() {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpg"));
        }

        protected override bool CanReadType(Type type) {
            return true;
        }

        protected override bool CanWriteType(Type type) {
            return base.CanWriteType(type);
        }

        protected override object OnReadFromStream(Type type, Stream stream, HttpContentHeaders contentHeaders) {
            throw new NotImplementedException();
        }

        protected override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, System.Net.TransportContext context) {
            //var person = value as Person;
            //if (person != null) {
            //    {
            //        var path = string.Format(CultureInfo.InvariantCulture, @"{0}bin\Images\Image{1}.jpg",
            //            AppDomain.CurrentDomain.BaseDirectory, (Convert.ToInt32(person.Id) % 3) + 1);
            //        using (var fileStream = new FileStream(path, FileMode.Open)) {
            //            var bytes = new byte[fileStream.Length];
            //            fileStream.Read(bytes, 0, (int)fileStream.Length);
            //            stream.Write(bytes, 0, (int)fileStream.Length);
            //        }
            //    }
            //}
        }

    }
}